﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Youz.Common.Helper
{
    public class CookieHelper
    {
        public CookieHelper() { }

        public static string KeyPrefix
        {
            get
            {
                return ConfigHelper.GetConfigString("KeyPrefix");
            }
        }

        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetCookie(string key, string value)
        {
            key = string.Format("{0}_{1}", KeyPrefix, key);
            HttpContext.Current.Response.SetCookie(new HttpCookie(key, DESEncrypt.Encrypt(value)));
        }

        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="minutes">过期时间</param>
        public static void SetCookie(string key, string value, int minutes)
        {
            key = string.Format("{0}_{1}", KeyPrefix, key);
            HttpCookie myCookie = new HttpCookie(key, DESEncrypt.Encrypt(value));
            myCookie.Expires = DateTime.Now.AddMinutes(minutes);
            HttpContext.Current.Response.SetCookie(new HttpCookie(key, DESEncrypt.Encrypt(value)));
        }

        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static string GetCookie(string key, bool isEncrypt = true)
        {
            key = string.Format("{0}_{1}", KeyPrefix, key);
            if (HttpContext.Current == null)
            {
                return string.Empty;
            }
            else if (HttpContext.Current.Request.Cookies[key] == null)
            {
                return string.Empty;
            }
            else if (string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[key].Value))
            {
                return string.Empty;
            }
            else
            {
                return isEncrypt ? DESEncrypt.Decrypt(HttpContext.Current.Request.Cookies[key].Value) : HttpContext.Current.Request.Cookies[key].Value;
            }
        }

        /// <summary>
        /// 清除cookie
        /// </summary>
        /// <param name="key">键</param>
        public static void ClearCookie(string key)
        {
            key = string.Format("{0}_{1}", KeyPrefix, key);
            HttpCookie myCookie = new HttpCookie(key);
            myCookie.Expires = DateTime.Today.AddDays(-100);
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }

        /// <summary>
        /// 清除所有cookies
        /// </summary>
        public static void ClearAllCookie()
        {
            HttpContext.Current.Response.Cookies.Clear();
            HttpCookie cookie = null;
            foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
            {
                cookie = new HttpCookie(key);
                cookie.Expires = DateTime.Today.AddDays(-100);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }
}
